package com.learn.sunday.stock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.LockSupport;

@Slf4j
public class Test2 {

    public static void main(String[] args) {




        AtomicBoolean flag = new AtomicBoolean(true);


        Thread consumeT = new Thread(()->{
            while (true) {
                LockSupport.park();
                log.info("消费");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                flag.set(true);

            }
        },"consume");

        consumeT.start();

        Thread produceT = new Thread(()->{
            while (true) {
                if (flag.get()) {

                    log.info("生产");
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    flag.set(false);
                    LockSupport.unpark(consumeT);
                }
            }
        },"produce");
        produceT.start();
    }
}
